package com.lwq.view;

import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

import com.lwq.controller.ProjectController;
import com.lwq.model.SuperTableItemModel;
import com.lwq.model.SuperTableModel;
import com.lwq.util.Constance;

public class SuperTableView extends JPanel {
	private SuperTableModel mSuperTableModel = null;
	private MatteBorder mTopBorder = null;
	private MatteBorder mBottomBorder = null;
	private GridBagConstraints mGridBagConstraints = null;
	private GridBagLayout mGridBagLayout = null;
	private int line_index = 0;
	private Map<SuperTableItemModel, Component> ComponentMaps = null;
	private ActionListener mExpActionListener = null;
	public SuperTableView() {
		super();
		init();
		setEvent();
	}

	private void init() {
		mTopBorder = new MatteBorder(1, 1, 1, 1, Color.black);
		mBottomBorder = new MatteBorder(0, 1, 1, 1, Color.black);
		mGridBagLayout = new GridBagLayout();
		mGridBagConstraints = new GridBagConstraints();
		mGridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		setEnabled(true);
		setVisible(true);
		setLayout(mGridBagLayout);
		ComponentMaps = new HashMap<SuperTableItemModel, Component>();
	}

	private void setEvent() {
		mExpActionListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg) {
				// TODO Auto-generated method stub
				ProjectController.editMemberCommand(arg.getActionCommand());
			}
		};
	}

	/**
	 * @param model
	 */
	public void reLoadData(SuperTableModel model) {
		if (model != null) {
			mSuperTableModel = model;
			removeAll();
			line_index = 0;
			for (int i = 0; i < model.getDatas().size(); i++) {
				if (i == 0) {
					addLine(model.getDatas().get(i), mTopBorder);
				} else {
					addLine(model.getDatas().get(i), mBottomBorder);
				}
			}
		}
	}

	public SuperTableModel getResultData() {
		SuperTableItemModel model = null;
		Iterator<SuperTableItemModel> it = ComponentMaps.keySet().iterator();
		JTextField StrValue = null;
		Choice ChoValue = null;
		while (it.hasNext()) {
			model = it.next();
			if (model.getType().equals(Constance.SUPER_TABLE_TEXT_TYPE)) {
				StrValue = (JTextField) ComponentMaps.get(model);
				model.setResult(StrValue.getText());
			} else if (model.getType()
					.equals(Constance.SUPER_TABLE_CHOICE_TYPE)) {
				ChoValue = (Choice) ComponentMaps.get(model);
				model.setResult(ChoValue.getSelectedItem());
			}
		}
		return mSuperTableModel;
	}

	private void addLine(SuperTableItemModel model, Border border) {
		JPanel panel = null;
		JLabel key = null;
		JTextField StrValue = null;
		Choice ChoValue = null;
		JButton btnValue = null;
		String[] list = null;
		if (model != null) {
			panel = new JPanel();
			panel.setEnabled(true);
			panel.setVisible(true);
			panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
			key = new JLabel(model.getKey());
			addComponent(key, 0, line_index, 1, 1);
			if (model.getType().equals(Constance.SUPER_TABLE_TEXT_TYPE)) {
				StrValue = new JTextField(10);
				StrValue.setText(model.getResult());
				addComponent(StrValue, 1, line_index, 1, 1);
				ComponentMaps.put(model, StrValue);
			} else if (model.getType()
					.equals(Constance.SUPER_TABLE_CHOICE_TYPE)) {
				list = model.getValue().split("\\|");
				ChoValue = new Choice();
				for (int i = 0; i < list.length; i++) {
					ChoValue.addItem(list[i]);
				}
				ChoValue.select(model.getResult());
				addComponent(ChoValue, 1, line_index, 1, 1);
				ComponentMaps.put(model, ChoValue);
			}else if (model.getType()
					.equals(Constance.SUPER_TABLE_EXP_TYPE)) {
				btnValue = new JButton("member");
				btnValue.setActionCommand(model.getKey());
				btnValue.addActionListener(mExpActionListener);
				addComponent(btnValue, 1, line_index, 1, 1);
				ComponentMaps.put(model, btnValue);
			}
			line_index++;
		}
	}
	
	public void addComponent(Component c, int x, int y, int width,
			int height) {
		mGridBagConstraints.gridx = x;
		mGridBagConstraints.gridy = y;
		mGridBagConstraints.gridheight = height;
		mGridBagConstraints.gridwidth = width;
		mGridBagLayout.setConstraints(c, mGridBagConstraints);
		add(c);
	}
}
